package crmdna.interaction;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.After;
import org.junit.Before;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.googlecode.objectify.ObjectifyFilter;

import crmdna.client.Client;
import crmdna.common.contact.ContactProp;
import crmdna.group.Group;
import crmdna.group.Group.GroupProp;
import crmdna.member.Member;
import crmdna.user.User;
import crmdna.user.User.Action;
import crmdna.user.User.ResourceType;
import crmdna.user.UserCore.UserProp;

public class InteractionTest {

	private final LocalServiceTestHelper datastoreHelper = new LocalServiceTestHelper(
	        new LocalDatastoreServiceTestConfig().setApplyAllHighRepJobPolicy());

	private final String client = "isha";
	private UserProp validUser;
	private UserProp validUser2;
	private final String userWithPermission = "withpermission@login.com";
	private long memberId;

	@Before
	public void setUp() {
		datastoreHelper.setUp();
		ObjectifyFilter.complete();

		Client.create(client);

		// can call getAll without any groups
		List<GroupProp> centers = Group.getAll(client);
		assertEquals(0, centers.size());

		GroupProp prop = Group.create(client, "Chennai", User.SUPER_USER);
		assertEquals(1, prop.groupId);

		validUser = User.create(client, "valid@valid.com", prop.groupId,
		        User.SUPER_USER);
		assertEquals(1, User.get(client, validUser.email).toProp().userId);

		validUser2 = User.create(client, "valid2@valid.com", prop.groupId,
		        User.SUPER_USER);
		assertEquals(2, User.get(client, validUser2.email).toProp().userId);

		User.create(client, userWithPermission, prop.groupId, User.SUPER_USER);
		assertEquals(3, User.get(client, userWithPermission).toProp().userId);

		User.addOrDeletePrivilege(client, userWithPermission,
		        ResourceType.INTERACTION, 0, Action.WRITE, true,
		        User.SUPER_USER);

		ContactProp contact = new ContactProp();
		contact.email = "email@email.com";
		memberId = Member.create(client, prop.groupId, contact, false,
		        User.SUPER_USER).memberId;
	}

	@After
	public void tearDown() {
		ObjectifyFilter.complete();
		datastoreHelper.tearDown();
	}

	// @Test
	// public void createTest() throws InterruptedException {
	//
	// Date date = new Date();
	//
	// //interaction type cannot be null
	// try {
	// Interaction.create(client, "interaction",
	// null, date, validUser.email);
	// assertTrue(false);
	// } catch (APIException ex) {
	// assertEquals("interaction type cannot be null",
	// Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, ex.statusCode);
	// }
	//
	// //interaction cannot be null or empty
	// try {
	// InteractionCore.create(client, null,
	// InteractionType.PHONE, date, validUser.userId);
	// assertTrue(false);
	// } catch (APIException ex) {
	// assertEquals("interaction cannot be null",
	// Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, ex.statusCode);
	// }
	//
	// try {
	// InteractionCore.create(client, "",
	// InteractionType.PHONE, date, validUser.userId);
	// assertTrue(false);
	// } catch (APIException ex) {
	// assertEquals("interaction cannot be empty string",
	// Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, ex.statusCode);
	// }
	//
	// StringBuilder bigString = new StringBuilder();
	// for (int i = 0; i < 100000; i++)
	// bigString.append("interaction ");
	//
	// try {
	// InteractionCore.create(client, bigString.toString(),
	// InteractionType.PHONE, date, validUser.userId);
	// assertTrue(false);
	// } catch (APIException ex) {
	// assertEquals("interaction string cannot be too big",
	// Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, ex.statusCode);
	// }
	//
	// InteractionProp interactionProp =
	// InteractionCore.create(client, "interaction 1",
	// InteractionType.PHONE, date, validUser.userId);
	// assertEquals("interaction id should be 1", 1,
	// interactionProp.interactionId);
	// assertEquals("interaction should be populated correctly",
	// "interaction 1", interactionProp.interaction);
	// assertEquals("interaction type should be populated correctly",
	// InteractionType.PHONE, interactionProp.interactionType);
	// assertEquals("date should be populated correctly",
	// interactionProp.interactionType, interactionProp.interactionType);
	// assertEquals("user id should be populated correctly",
	// validUser.userId, interactionProp.userId);
	//
	// List<InteractionProp> props = InteractionCore.get(client,
	// interactionProp.interactionId);
	// assertEquals(1, props.size());
	// assertEquals("first interaction should have id 1", 1,
	// props.get(0).interactionId);
	// assertEquals("interaction string should be populated correctly",
	// "interaction 1", props.get(0).interaction);
	// assertEquals("interaction type should be populated correctly",
	// InteractionType.PHONE, props.get(0).interactionType);
	// assertEquals("date should be populated correctly",
	// date, props.get(0).timestamp);
	// assertEquals("user id should be populated correctly",
	// validUser.userId, props.get(0).userId);
	//
	// //null as date should populate current timestamp
	// interactionProp =
	// InteractionCore.create(client, "interaction 2",
	// InteractionType.PHONE, null, validUser.userId);
	// assertEquals("interaction id should be 2", 2,
	// interactionProp.interactionId);
	// assertTrue("timestamp should be current date",
	// interactionProp.timestamp.getTime() < date.getTime() + 50);
	// assertTrue("timestamp should be current date",
	// interactionProp.timestamp.getTime() > date.getTime() - 50);
	// }
	//
	//
	// @Test
	// public void deleteTest() {
	//
	// //interaction id should be valid
	// try {
	// InteractionCore.delete(client, 100); //invalid interaction id
	// assertTrue(false);
	// } catch (APIException e) {
	// assertEquals("interaction id should exist",
	// Status.ERROR_RESOURCE_DOES_NOT_EXIST, e.statusCode);
	// }
	//
	// Date date1 = new Date();
	// InteractionProp interactionProp =
	// InteractionCore.create(client, "interaction 1",
	// InteractionType.EMAIL, date1, validUser.userId);
	// assertEquals("interaction id assigned in sequence",
	// 1, interactionProp.interactionId);
	//
	// InteractionCore.delete(client, interactionProp.interactionId);
	// assertEquals("cannot get after deleting",
	// 0, InteractionCore.get(client, memberId).size());
	// }
	//
	// @Test
	// public void updateTest() {
	// Date date = new Date();
	//
	// long interactionId = InteractionCore.create(client, "interaction 1",
	// InteractionType.EMAIL, date, validUser.userId).interactionId;
	//
	// try {
	// InteractionCore.update(client, 100, //invalid interaction id
	// "interaction",
	// InteractionType.PHONE, date, validUser.userId);
	// assertTrue(false);
	// } catch (APIException e) {
	// assertEquals("interaction id should be valid",
	// Status.ERROR_RESOURCE_DOES_NOT_EXIST, e.statusCode);
	// }
	//
	// //interaction type cannot be null
	// try {
	// InteractionCore.update(client, interactionId, "interaction",
	// null, date, validUser.userId);
	// assertTrue(false);
	// } catch (APIException ex) {
	// assertEquals("interaction type cannot be null",
	// Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, ex.statusCode);
	// }
	//
	// //interaction cannot be null or empty
	// try {
	// InteractionCore.update(client, interactionId, null,
	// InteractionType.PHONE, date, validUser.userId);
	// assertTrue(false);
	// } catch (APIException ex) {
	// assertEquals("interaction cannot be null",
	// Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, ex.statusCode);
	// }
	//
	// try {
	// InteractionCore.update(client, interactionId, "",
	// InteractionType.PHONE, date, validUser.userId);
	// assertTrue(false);
	// } catch (APIException ex) {
	// assertEquals("interaction cannot be empty string",
	// Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, ex.statusCode);
	// }
	//
	// StringBuilder bigString = new StringBuilder();
	// for (int i = 0; i < 100000; i++)
	// bigString.append("interaction ");
	//
	// try {
	// InteractionCore.update(client, interactionId, bigString.toString(),
	// InteractionType.PHONE, date, validUser.userId);
	// assertTrue(false);
	// } catch (APIException ex) {
	// assertEquals("interaction string cannot be too big",
	// Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, ex.statusCode);
	// }
	//
	// InteractionProp interactionProp =
	// InteractionCore.update(client, interactionId, "interaction 1",
	// InteractionType.PHONE, date, validUser.userId);
	// assertEquals("interaction id should be retained after update",
	// interactionId, interactionProp.interactionId);
	// assertEquals("interaction should be populated correctly",
	// "interaction 1", interactionProp.interaction);
	// assertEquals("interaction type should be populated correctly",
	// InteractionType.PHONE, interactionProp.interactionType);
	// assertEquals("date should be populated correctly",
	// interactionProp.interactionType, interactionProp.interactionType);
	// assertEquals("user id should be populated correctly",
	// validUser.userId, interactionProp.userId);
	//
	// List<InteractionProp> props = InteractionCore.get(client,
	// interactionProp.interactionId);
	// assertEquals(1, props.size());
	// assertEquals("first interaction should have id 1", 1,
	// props.get(0).interactionId);
	// assertEquals("interaction string should be populated correctly",
	// "interaction 1", props.get(0).interaction);
	// assertEquals("interaction type should be populated correctly",
	// InteractionType.PHONE, props.get(0).interactionType);
	// assertEquals("date should be populated correctly",
	// date, props.get(0).timestamp);
	// assertEquals("user id should be populated correctly",
	// validUser.userId, props.get(0).userId);
	//
	// //null as date should populate current timestamp
	// interactionProp =
	// InteractionCore.create(client, "interaction 2",
	// InteractionType.PHONE, null, validUser.userId);
	// assertEquals("interaction id should be 2", 2,
	// interactionProp.interactionId);
	// assertTrue("timestamp should be current date",
	// interactionProp.timestamp.getTime() < date.getTime() + 50);
	// assertTrue("timestamp should be current date",
	// interactionProp.timestamp.getTime() > date.getTime() - 50);
	// }
}
